﻿using API;
using API.Store;
using Common;
using DevExpress.Xpo;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    public class ProviderContactBLL : BaseBLL
    {
        public void BatchInsert(DateTime? lastUpdateDate = null)
        {
            try
            {
                this.Request.LastUpdateDate = lastUpdateDate;
                ClientRequest request = this.StoreRequest();
                ApiStore dataProxy = ServiceProxy.ApiStoreProxy();
                MB_ProviderContact[] pcs = dataProxy.GetProviderContacts(request);
                if (pcs == null || pcs.Length <= 0) return;
                using (UnitOfWork uow = DatabaseSchema.UnitOfWork)
                {
                    foreach (MB_ProviderContact p in pcs)
                    {
                        B_ProviderContact pc = new B_ProviderContact(uow);
                        pc.ProviderContactID = p.ProviderContactID;
                        pc.ProviderID =uow.GetObjectByKey<B_Provider>(p.ProviderID);
                        pc.ContactName = p.ContactName;
                        pc.Phone = p.Phone;
                        pc.Fax = p.Fax;
                        pc.Sex = p.Sex;
                        pc.Sort = p.Sort;
                        pc.Creator = p.Creator;
                        pc.CreateDT = p.CreateDT;
                        pc.Operator = p.Operator;
                        pc.UpdateDT = p.UpdateDT.Value;
                        pc.Remark = p.Remark;
                        pc.Save();
                    }
                    uow.CommitChanges();
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("ProviderContactBLL.BatchInsert", ex);
            }
        }
        public DateTime MaxUpdateDate()
        {
            DateTime updateDT = DateTime.MinValue;
            string sql = "select Max(UpdateDT) from B_ProviderContact";
            updateDT = Convert.ToDateTime(DatabaseSchema.UnitOfWork.ExecuteScalar(sql));
            return updateDT;
        }
    }
}
